package com.woniuxy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Drug;
import com.woniuxy.mapper.DrugMapper;
import com.woniuxy.service.DrugService;
import com.woniuxy.service.SaasService;
import com.woniuxy.util.PageParam;
import com.woniuxy.util.ResponseData;
import com.woniuxy.util.ResponseEnum;
import com.woniuxy.util.SnowflakeIdGenerator;
import io.netty.util.internal.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.UUID;

@Service
public class DrugServiceImpl implements DrugService, SaasService {

    @Resource
    private DrugMapper drugMapper;

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public ResponseData<?> queryList(PageParam<Drug> pageParam) {

        PageHelper.startPage(pageParam.getPageNum(),pageParam.getPageSize());
        List<Drug> drugList = drugMapper.queryList(pageParam.getParam());

        return new ResponseData<>().ok(new PageInfo<>(drugList));
    }

    @Override
    public ResponseData<?> generateToken(){
        //生成唯一的token 存入redis中
        String uuid = UUID.randomUUID().toString();
        redisTemplate.opsForValue().set("drugToken",uuid);
        return new ResponseData<>().ok(uuid);
    }

    @Override
    public ResponseData<?> addDrug(Drug drug) {
        if (StringUtils.isBlank(drug.getDsName())||StringUtils.isBlank(drug.getDsBrand())||
            StringUtils.isBlank(String.valueOf(drug.getDsInPrice()))||drug.getDsInPrice() == null ||
            StringUtils.isBlank(String.valueOf(drug.getDsOutPrice())) || drug.getDsOutPrice() == null ||
            StringUtils.isBlank(String.valueOf(drug.getDsStock())) || drug.getDsStock() ==null ||
            StringUtils.isBlank(drug.getDsStatus()) || StringUtils.isBlank(drug.getDsIsPrescription())||
            StringUtils.isBlank(drug.getDsReimbursement())||StringUtils.isBlank(drug.getToken())){
            return new ResponseData<>().fail(ResponseEnum.MANDATORY_EMPTY);
        }


        try {
            //查看唯一Token是否存在
            String token = (String) redisTemplate.opsForValue().get("drugToken");
            if (!drug.getToken().equals(token)){
                return new ResponseData<>().fail(ResponseEnum.NO_TOKEN);
            }

            SnowflakeIdGenerator sfig = new SnowflakeIdGenerator();
            long l = sfig.nextId();
            String dsNo = "DRUG" +l;
            drug.setDsNo(dsNo);
            if (drug.getDsReimbursement().equals("A")){
                drug.setDsReimbursementRatio(BigDecimal.valueOf(1.0));
            } else if(drug.getDsReimbursement().equals("B")){
                Random random = new Random();
                int i = random.nextInt(10) + 5;
                BigDecimal tatio = BigDecimal.valueOf(i / 100);
                drug.setDsReimbursementRatio(tatio);
            } else if(drug.getDsReimbursement().equals("C")){
                drug.setDsReimbursementRatio(BigDecimal.valueOf(0));
            }
            drugMapper.insertDrug(drug);


            return new ResponseData<>().ok();
        } catch (InterruptedException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        } finally {
            //不管是添加成功 还是没有添加 都清除
            redisTemplate.delete("drugToken");
        }
    }


    @Override
    public ResponseData<?> queryDrugById(Integer id) {
        Drug drug = drugMapper.queryDrugById(id);
        if (drug ==null){
            return new ResponseData<>().fail(ResponseEnum.EMPTY);

        }
        return new ResponseData<>().ok(drug);
    }


    @Override
    public ResponseData<?> updateDrug(Drug drug) {
        if (drug.getDsReimbursement().equals("A")){
            drug.setDsReimbursementRatio(BigDecimal.valueOf(1.0));
        } else if(drug.getDsReimbursement().equals("B")){
            Random random = new Random();
            int i= random.nextInt(10) + 5;
            BigDecimal numerator = new BigDecimal(i); //分子
            BigDecimal denominator = new BigDecimal("100"); //分母
            BigDecimal tatio = numerator.divide(denominator, 2, BigDecimal.ROUND_HALF_UP);

            drug.setDsReimbursementRatio(tatio);
        } else if(drug.getDsReimbursement().equals("C")){
            drug.setDsReimbursementRatio(BigDecimal.valueOf(0));
        }
        drugMapper.updateDrug(drug);
        return new ResponseData<>().ok();
    }


    @Override
    public ResponseData<?> deleteDrug(Integer id) {
        Drug drug = new Drug();
        drug.setId(id);
        drug.setStatus("1");
        drugMapper.updateDrug(drug);
        return new ResponseData<>().ok();
    }

    @Override
    public ResponseData<?> queryDrugList(Drug drug) {
        List<Drug> drugs = drugMapper.queryList(drug);
        return new ResponseData<>().ok(drugs);
    }
}
